<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Request;
use think\Db;
use think\Cache;
use think\Log;
use fast\Http;

/**
 * 会员接口
 */
class Qiwu extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

    /**
     * 苏格拉底接口
     *
     * @ApiMethod (POST)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function get_qiwu_api($msg,$uid)
    {
        $appkey_find=Db::table("fa_sgld_setting")->where("key_name='appkey'")->find();
        $appkey=$appkey_find["key_value"];
        $appsecret_find=Db::table("fa_sgld_setting")->where("key_name='appsecret'")->find();
        $appsecret=$appsecret_find["key_value"];
        $url_find=Db::table("fa_sgld_setting")->where("key_name='qiwu_url'")->find();
        $url = $url_find["key_value"];
        $type= $this->request->post('type',0);// 0: 输入的题  2:输入回答的问题
       /* $msg= $this->request->post('msg');
        $uid= $this->request->post('uid');*/
        $post_data['appkey'] = $appkey;
        $post_data['timestamp'] =time();
        $post_data['uid'] = $uid;
        $post_data['verify'] = MD5($appsecret.$post_data['uid'].$post_data['timestamp']);
        //var_dump($post_data['verify'] );die;
        $post_data['msg'] =$msg;
        $post_data['sn'] ="aaa5";
        $ret = json_decode(Http::post($url, $post_data), true);
        return $ret;
        $this->success(__('获取成功'), $ret);

    }



    //生成唯一编号
    public  function getExtensionCode()
    {
        $code = self::generate_password(5);
        $where["uid"]=$code;
        $others_is_have_bianma = Db::table("fa_sgld_message")->where($where)->find();
        if ($others_is_have_bianma) {
            //如果生成的邀请码存在，继续生成，直到不存在
            $code = self::getExtensionCode();
        }
        return $code;
    }
    public static function generate_password($length = 5)
    {
        $chars = '123456789';
        $password = "";
        for ($i = 0; $i < $length; $i++) {
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }


}
